%macro get_f1099r;

	
	proc printto log = "&logdir/get_f1099r.txt" new;
	run;
	

		/****************************
		*	Get the tins 			*
		*	into 'tinlist'			*
		****************************/
		%make_tinlist;

		/****************************************
		*										*
		*			Start PROC SQL				*
		*										*
		****************************************/
		proc sql noprint;
		[connection details redacted]
			create table f1099r as
			select * from connection to iq 
			(
			select
				a.[redacted] as person_id,
				sum(case when a.[redacted] = "1" then a.[redacted]
					else a.[redacted]) as pensions,
				max(case when a.[redacted] = "J" 
					or a.[redacted] = "J" then 1
					else 0 end) as early_roth,
				max(case when a.[redacted] = "1" and [redacted] ~= "1" then 1 else 0 end)
					as t_unkn,
				sum(a.[redacted]) as f1099r_wh,
				a.[redacted]-&base_yr as rel_time
				from [redacted] as a
					inner join #tinlist as b
						on(a.[redacted] = b.person_id)
				where a.[redacted] - &base_yr in(-2,-1,0)
					and a.[redacted] <= 4
				group by a.[redacted], rel_time
				order by a.[redacted], rel_time
			);
			disconnect from iq;
			quit;


		data same_year;
			set f1099r;
			if rel_time = 0;
			drop rel_time;
		run;
		
		proc means data = f1099r nway noprint;
			class person_id;
			output out = sum_dist(drop=_type_ _freq_)
				sum(pensions) = dist_sav_cred;
		run;
		
		data rlib.f1099r;
			merge same_year sum_dist;
			by person_id;
		run;
		

		

	proc printto;
	run;
%mend;
